package com.nf.health.app.mine.activity;

import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.utils.KeyBoardManager;
import com.nf.health.app.utils.PreferenceHelper;
import com.nf.health.app.utils.Util;

/** 
* @ClassName: ModifyNickNameActivity 
* @Description: 用户信息修改页面
* @author Ian 
* @date 2015-11-4 上午9:56:06 
*  
*/
public class ModifyNickNameActivity extends BaseActivity implements
		OnClickListener {
	public static final int TYPE_MODIFY_NICKNAME = 0;
	public static final int TYPE_MODIFY_IDENTIFY = 1;
		public static final int TYPE_MODIFY_RNAME = 2;
	private EditText ed_nickName;
	private int type;
	private TextView titlebar_title_tv;
	private ImageView titlebar_right_view;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		type = getIntent().getIntExtra("type",0);
		setTitleBar(R.layout.titlebar_base);
		setContent(R.layout.activity_modify_nick_name);
		initView();
		initData();
		initListener();
	}

	private void initView() {
		titlebar_title_tv = (TextView) findViewById(R.id.titlebar_title_tv);
		ed_nickName = (EditText) findViewById(R.id.ed_nickName);
		titlebar_right_view = (ImageView) findViewById(R.id.titlebar_right_view);
		titlebar_right_view.setImageResource(R.drawable.ic_title_send);
	}

	private void initData() {
		if (type == TYPE_MODIFY_NICKNAME) {
			ed_nickName.setInputType(InputType.TYPE_CLASS_TEXT);
			titlebar_title_tv.setText("修改昵称");
			String username = PreferenceHelper.getString(PreferenceHelper.USERNICK, "");
			if(!TextUtils.isEmpty(username)){
				ed_nickName.setText(username);
				ed_nickName.setSelection(username.length());
			}
		} else if(type == TYPE_MODIFY_IDENTIFY){
			titlebar_title_tv.setText("修改身份证号");
			String value = getIntent().getStringExtra("value");
			ed_nickName.setHint("请输入身份证号");
			if(!TextUtils.isEmpty(value)){
				ed_nickName.setText(value);
				ed_nickName.setSelection(value.length());
			}
		} else if(type == TYPE_MODIFY_RNAME){
			titlebar_title_tv.setText("真实姓名");
			String value = getIntent().getStringExtra("value");
			ed_nickName.setHint("请输入真实姓名~");
			if(!TextUtils.isEmpty(value)){
				if(value.equals("未填写")){
					ed_nickName.setText("");
				}else{
					ed_nickName.setText(value);
					ed_nickName.setSelection(value.length());
				}
			}
		}
	}

	private void initListener() {
		titlebar_right_view.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.titlebar_right_view:
			KeyBoardManager.closeKeyboard(this, ed_nickName);
			String value = ed_nickName.getText().toString();
			if (!TextUtils.isEmpty(value)) {
				if (type == TYPE_MODIFY_NICKNAME) {
					if(value.length()>10){
						showToast("昵称不超过10个字");
						return;
					}
					if(!Util.isNick(value)){
						showToast(R.string.nick_prompt);
						return;
					}
					provider.requestUpdateUser(value, null, null, null, null, null, null, null, null, "updateUser");
				}else if(type == TYPE_MODIFY_RNAME) {
					if(value.length()>10){
						showToast("真实姓名不超过10个字");
						return;
					}
					if(!Util.isNick(value)){
						showToast("请输入正确的姓名~");
						return;
					}
					provider.requestUpdateUser(null, null, null, null, null, null, null, null, value, "updateUser");
				}else{
					if(!Util.isIDCARD(value)){
						showToast("身份证号不正确");
						return;
					}
					provider.requestUpdateUser(null,null, null, null, null, null,value,null,null,"updateUser");
					//TODO
				}
			} else {
				if (type == TYPE_MODIFY_NICKNAME) {
					showToast("请输入昵称");
				}else if(type == TYPE_MODIFY_RNAME) {
					showToast("请输入真实姓名");
				}else{
					showToast("请输入身份证号");
				}
			}
			break;
		}
		super.onClick(v);
	}

	@Override
	public void handleActionSuccess(String actionName, Object object) {
		super.handleActionSuccess(actionName, object);
		if (actionName.equals("updateUser")) {
			showToast("修改成功");
			finish();
		}
	}
}
